package com.baozun.houji.goods.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baozhun.mall.common.model.bean.ApiPagerResponse
import com.baozhun.mall.common.model.bean.CategoryInfoBean
import com.baozhun.mall.common.model.bean.GoodsInfoBean
import com.baozhun.mall.common.model.bean.TemplateInfoBean
import com.baozun.houji.goods.network.homeApiService
import com.scwang.smart.refresh.layout.api.RefreshLayout
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：CategoryListViewModel
 * @Description：{@link }
 * @Author：xing.luo
 * @Since：2022/11/09
 * @Version：1.0.0
 */
class CategoryListViewModel : BaseViewModel() {
    var templateList: MutableList<TemplateInfoBean> = mutableListOf()
    var categoryGoodsInfo = MutableLiveData<ApiPagerResponse<ArrayList<GoodsInfoBean>>>()
    var mShowThreeLevelCategory = MutableLiveData(false)
    var mShowCategoryIndicator = MutableLiveData(true)
    var category: CategoryInfoBean? = null
    var mCurrentPage: Int = 1
    private var mUpdatedAt: String? = null //每页最后一个item的更新时间

    fun getCategoryGoodsList(
        isRefresh: Boolean,
        categoryId: String,
        sort: Int? = null,
        sort_order: Int? = null,
        refreshLayout: RefreshLayout? = null,
    ) {
        if (isRefresh) mCurrentPage = 1
        request({ homeApiService.getCategoryGoodsData(categoryId, sort, sort_order, mCurrentPage, mUpdatedAt) }, {
            if (mCurrentPage == 1) {
                it.data.firstOrNull()?.let {
                    mUpdatedAt = it.updated_at
                }
            }
            mCurrentPage += 1
            categoryGoodsInfo.postValue(it)
        }, {
            refreshLayout?.finishLoadMore()
        })
    }
}